package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.ReservationSearchDetailsModel;
import com.jiayang.hhotel.bean.ReservationSearchModel;
import com.jiayang.hhotel.bean.RoomNumModel;
import com.jiayang.hhotel.presenter.ReservationSearchDetailsPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.ScreenUtil;

import java.util.List;

import cn.droidlover.xdroidmvp.custom.TitleListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/23 0023
 * 订房查询详情页面.
 */


public class ReservationSearchDetailsActivity extends XActivity<ReservationSearchDetailsPresenter> implements TitleListener, View.OnClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private TextView orderInfo,//订单信息显示
                    name,//名称
                    agreeGuest,//协议客户
                    contactName,//联系人姓名
                    contactTel;//联系人电话
    private ImageView zhong,//钟点房
            vip,//vip
            ling,//领
            chuang,//床
            yu;//预
    private LinearLayout addReservationInfo;//添加订房信息容器
    private LinearLayout addAdditionalInfo;//添加附加信息容器

    private ScrollView sv;//滚动布局

    private Button mDisCount;//折扣按钮

    private String[] addInfoTitle;//附加信息的标题数组
    private String[] addInfoContent;//附加信息的内容数组

    private ReservationSearchModel.PRsvbillRoomListDtosBean project;//上个页面传递过来的对象
    private ReservationSearchDetailsModel.PRsvbillRoomDetailListDtosBean dividePro;//点击分房按钮所在的对象

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.see_details),R.color.orange_color,getString(R.string.editor),this);
        project = (ReservationSearchModel.PRsvbillRoomListDtosBean) getIntent().getExtras().getSerializable(Constant.INTENT_PROJECT_KEY);

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        orderInfo= (TextView) findViewById(R.id.tv_order_info);
        zhong= (ImageView) findViewById(R.id.iv_zhong);
        vip= (ImageView) findViewById(R.id.iv_vip);
        ling= (ImageView) findViewById(R.id.iv_ling);
        chuang= (ImageView) findViewById(R.id.iv_chuang);
        yu= (ImageView) findViewById(R.id.iv_yu);

        sv = (ScrollView) findViewById(R.id.sl);

        name= (TextView) findViewById(R.id.tv_name);
        agreeGuest= (TextView) findViewById(R.id.tv_agree_guest);
        contactName= (TextView) findViewById(R.id.tv_contact_name);
        contactTel= (TextView) findViewById(R.id.tv_contact_tel);

        addReservationInfo= (LinearLayout) findViewById(R.id.add_info);
        addAdditionalInfo= (LinearLayout) findViewById(R.id.add_additional_info);

        addInfoTitle = getResources().getStringArray(R.array.reservation_search_details_add_info);
        addInfoContent = new String[addInfoTitle.length];
        addInfoContent[0] = project.getArrivedIn()+"";//到店方式
        addInfoContent[1] = project.getArrivedTime();//到店时间
        addInfoContent[2] = project.getPickUpIn() == 0?"不接送":"接送";//是否接送
        addInfoContent[3] = project.getLeaveIn();//离开方式
        addInfoContent[4] = project.getLeaveTime();//离开时间
        addInfoContent[5] = project.getPickUpOut() == 0?"不接送":"接送";//是否接送
        addInfoContent[6] = project.getOrgdescp();//客源描述
        addInfoContent[7] = project.getMarketdescp();//市场

        mDisCount= (Button) findViewById(R.id.bt_discount);
        sv.setVisibility(View.GONE);
        mDisCount.setVisibility(View.GONE);
        getList();

        mDisCount.setOnClickListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_reservation_search_details;
    }

    @Override
    public ReservationSearchDetailsPresenter newP() {
        return new ReservationSearchDetailsPresenter();
    }

    public void getList(){
        CommonUtil.showProgressDialog(this,R.string.loading);
        getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE, project.getCRONum());
    }

    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        CommonUtil.dismissProgressDialog(this);
        layout.setVisibility(View.VISIBLE);
        switch (state){
            case 1002://没有数据
                break;
            case -1://服务器错误
                icon.setImageResource(R.mipmap.ic_launcher);
                tv.setText(getString(R.string.service_error));
                icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        layout.setVisibility(View.GONE);
                        getList();
                    }
                });
                break;
            default://其他错误
                icon.setImageResource(R.mipmap.ic_launcher);
                tv.setText(getString(R.string.net_error));
                icon.setOnClickListener(null);
                break;
        }
    }

    /**
     * 添加订房信息
     */
    public void addReservationInfo(List<ReservationSearchDetailsModel.PRsvbillRoomDetailListDtosBean> models){
        addReservationInfo.removeAllViews();
        for(int i=0;i<models.size();i++){
            View itemView= LayoutInflater.from(this).inflate(R.layout.reservation_search_details_reservation_info_item,null);
            Button divide= (Button) itemView.findViewById(R.id.bt_divide);
            TextView toStore= (TextView) itemView.findViewById(R.id.tv_to_store);
            TextView diparture= (TextView) itemView.findViewById(R.id.tv_departure);
            TextView roomLevel= (TextView) itemView.findViewById(R.id.tv_room_level);
            TextView roomPrice= (TextView) itemView.findViewById(R.id.tv_room_price);
            TextView roomCount= (TextView) itemView.findViewById(R.id.tv_room_count);
            ReservationSearchDetailsModel.PRsvbillRoomDetailListDtosBean model=models.get(i);
            toStore.setText(getString(R.string.daodian_date)+getString(R.string.space)+model.getArrDate());
            diparture.setText(getString(R.string.lidian_date)+getString(R.string.space)+model.getDptDate());
            roomLevel.setText(getString(R.string.room_grade)+getString(R.string.space)+model.getRmType());
            roomPrice.setText(getString(R.string.room_price_complete)+getString(R.string.space)+model.getFolioRate());
            roomCount.setText(getString(R.string.room_count_complete)+getString(R.string.space)+model.getRmNumbers());
            divide.setTag(model);
            divide.setOnClickListener(this);
            addReservationInfo.addView(itemView);
        }
        orderInfo.setText(getString(R.string.order_info)+getString(R.string.space)+project.getCRONum());
        name.setText(getString(R.string.name)+getString(R.string.space)+project.getName());
        agreeGuest.setText(getString(R.string.agree_guest)+getString(R.string.space)+project.getTaname());
        contactName.setText(getString(R.string.contact_person_name)+getString(R.string.space)+project.getRsvgstname());
        contactTel.setText(getString(R.string.contact_person_tel)+getString(R.string.space)+project.getRsvphone());
        sv.setVisibility(View.VISIBLE);
        mDisCount.setVisibility(View.VISIBLE);

        addAdditionalInfo.removeAllViews();
        for(int i = 0;i < addInfoTitle.length; i++){
            additionalInfo(addInfoTitle[i], addInfoContent[i]);
        }
        CommonUtil.dismissProgressDialog(this);
    }

    /**
     * 添加附加信息
     */
    private void additionalInfo(String title,String content){
        TextView itemView = new TextView(this);
        itemView.setTextColor(getResources().getColor(R.color.white));
        itemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        itemView.setPadding(ScreenUtil.dip2px(this,20),ScreenUtil.dip2px(this,15),0,0);
        itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        itemView.setText(title + getString(R.string.space) + content);
        addAdditionalInfo.addView(itemView);
    }

    @Override
    public void rightClickListener() {
//        Intent start = new Intent(this,ReservationSearchEditorActivity.class);
//        startActivity(start);
    }

    private int tag = -1;//上次点击的折扣项
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_discount://折扣
                getP().createDisCountDialog(this,tag);
                break;
            case R.id.bt_divide://分房
                dividePro = (ReservationSearchDetailsModel.PRsvbillRoomDetailListDtosBean) v.getTag();
                getP().initRoomList(Constant.LAUGUAGE_FLAG_CHINESE,dividePro.getArrDate(),dividePro.getDptDate(),1,dividePro.getRmType());
                break;
        }
    }

    //保存折扣对话框
    public void saveDisCount(int mpGrade){
        tag = mpGrade;
        getP().disCountSave(Constant.LAUGUAGE_FLAG_CHINESE,project.getCRONum(), "",mpGrade);//此处没有房号
    }

    public void initDivideDialog(List<RoomNumModel.PRsvforcastRoomDtoListBean> list){
        getP().createDivideDialog(this,list,dividePro.getRmNumbers());
    }

    /**
     * 点击确定按钮
     * @param room 房号
     */
    public void clickDeter(String room){
        getP().deterMineDivide(Constant.LAUGUAGE_FLAG_CHINESE,dividePro.getSerial(),project.getCRONum(),room);
    }
}
